﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Context;
using Entities;
using WCF_ListenMusic.DataContract;
namespace WCF_ListenMusic.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SongService" in code, svc and config file together.
    public class SongService : ISongService
    {
       
        #region ISongService Members
        Connect_DBDataContext context = new Connect_DBDataContext();
        public DataContract_Song getSongByID(int SongID)
        {
            try
            {
                if (SongID <= 0)
                {
                    throw new FaultException("Parameter is zero or nagative");
                }
                else
                {
                    var q = from s in context.Songs
                            where s.SongID.Equals(SongID)
                            select new DataContract_Song
                            {
                                SingerID = Convert.ToInt32(s.SingerID),
                                SongID = Convert.ToInt32(s.SongID),
                                SongName = s.SongName,
                                SongURL = s.SongURL,
                                TypeID = Convert.ToInt32(s.TypeID),
                                CategoryID = Convert.ToInt32(s.CategoryID),
                                MusicianID = Convert.ToInt32(s.MusicianID),
                                MusicianName = s.Musician.MusicianName,
                                CategoryName = s.Category.CategoryName,
                                SingerName = s.Singer.SingerName,
                                AlbumID = Convert.ToInt32(s.AlbumID),
                                AlbumName = s.Album.AlbumName,
                            };
                    return q.SingleOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region ISongService Members


        public List<Song> getSongByAlbumID(int AlbumID)
        {
            try
            {
                if (AlbumID <= 0)
                {
                    throw new FaultException("Parameter is zero or nagative");
                }
                else
                {
                    var q = from s in context.Songs
                            where s.AlbumID.Equals(AlbumID)
                            select s;
                    return q.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region ISongService Members


        public List<Song> getSongByType(int TypeID)
        {
            try
            {
                if (TypeID <= 0)
                {
                    throw new FaultException("Parameter is zero or nagative ");
                }
                else
                {
                    var q = from s in context.Songs                            
                            where s.SongID.Equals(TypeID)
                            select s;
                    return q.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
        
        #region ISongService Members
        public List<DataContract_Song> getTopSong(int number, int type)
        {
            try {
                if (number <= 0)
                {
                    throw new FaultException("Parameter is zero or nagative ");
                }
                else
                { 
                    var q = (from s in context.Songs
                            orderby s.CountView descending
                            where s.TypeID.Equals(type)
                            select new DataContract_Song
                            {
                                SingerID = Convert.ToInt32(s.SingerID),
                                SongID = Convert.ToInt32(s.SongID),
                                SongName = s.SongName,
                                SongURL = s.SongURL,
                                TypeID = Convert.ToInt32(s.TypeID),
                                SingerName = s.Singer.SingerName,
                            }).Take(number);
                    return q.ToList();
                }
            }
            catch (Exception ex) 
            { 
                throw new FaultException(ex.Message); 
            }
        }
        #endregion


        public List<DataContract_Song> getnewSong_Singer(int number)
        {
            try
            {
                if (number <= 0)
                {
                    throw new FaultException("Parameter is zero or nagative ");
                }
                else
                {
                    var q = (from s in context.Songs
                             where s.DateUp.Date.Month.Equals(DateTime.Today.Date.Month - 1) && s.DateUp.Date.Year.Equals(DateTime.Today.Date.Year)
                             orderby s.CountView descending
                             select new DataContract_Song
                    {
                        SingerID = Convert.ToInt32(s.SingerID),
                        SongID = Convert.ToInt32(s.SongID),
                        SongName = s.SongName,
                        SongURL = s.SongURL,                       
                        MusicianName = s.Musician.MusicianName,
                        TypeID = Convert.ToInt32(s.TypeID),
                        Lyrics = s.Lyrics,
                        AlbumID = Convert.ToInt32(s.AlbumID),
                        MemberID = Convert.ToInt32(s.MemberID),
                        CategoryID = Convert.ToInt32(s.CategoryID),
                        CountView = Convert.ToInt32(s.CountView),
                        DateUp = Convert.ToDateTime(s.DateUp),
                        CountryID = Convert.ToInt32(s.CountryID),
                        SingerName = s.Singer.SingerName,
                    }).Take(number);
                    return q.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
        public List<Song> getAllSong()
        {
            try {
                var q = from c in context.Songs                        
                        select c;                
                return q.ToList();
            }
            catch (Exception ex)
            { 
                throw new FaultException(ex.Message);
            }
        }

        #region ISongService Members


        public List<Song> getSongBySingerName(string singerName)
        {
            try
            {
                if (singerName == "")
                {
                    throw new FaultException("Parameter is null");
                }
                else
                {
                    var q = from s in context.Songs
                            where s.Singer.SingerName.Contains(singerName)
                            select s;
                    return q.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }
}
